package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Document;
import com.kaishengit.entity.Employee;
import com.kaishengit.entity.Goal;
import com.kaishengit.util.db.DBHelp;
import com.kaishengit.util.db.Page;
import com.kaishengit.util.db.RowMapper;


public class DocumentDao {
	private DBHelp<Document> db = new DBHelp<Document>();
	public void save(Document document) {
		String sql="insert into t_document(id,title,content,employeeid,projectid) values(?,?,?,?,?)";
		db.executeSQL(sql, document.getId(),document.getTitle(),document.getContent(),document.getEmployeeid(),document.getProjectid());
	}
	public int count(String projectid) {
		String sql = "SELECT COUNT(*) FROM t_document WHERE projectid=? ";
		return db.executeQueryToNumber(sql,projectid).intValue();
	}
	public Page<Document> findByPageAndProjectId(int pageNum,String projectid) {
		Page<Document> page = new Page<Document>(count(projectid), pageNum);
		String sql="select t_document.id,`title`,content,createtime,employeeid,projectid,username from t_document,t_employee where t_document.employeeid=t_employee.id and projectid =? limit ?,?";
		List<Document> list = db.executeQueryToList(sql, 
		new RowMapper<Document>(){
			public Document mapRow(ResultSet rs) throws SQLException {
				Document d = new Document();
				d.setId(rs.getString("id"));
				d.setTitle(rs.getString("title"));
				d.setContent(rs.getString("content"));
				d.setCreatetime(rs.getString("createtime"));
				d.setEmployeeid(rs.getString("employeeid"));
				d.setProjectid(rs.getString("projectid"));
				
				Employee  e = new Employee();
				e.setId(rs.getString("employeeid"));
				e.setUsername(rs.getString("username"));
				
				d.setEmployee(e);
				
				return d;
			}
		},projectid,page.getStart(),page.getPageSize());
		page.setList(list);
		return page;
			
		
	}
	public Document findById(String id) {
		String sql = "select id ,title,content,createtime,projectid,employeeid from t_document where id = ?";
		return db.executeQueryToObject(sql, new DocumentRowMapper(), id);
	}
	private class DocumentRowMapper implements RowMapper<Document> {

		public Document mapRow(ResultSet rs) throws SQLException {
			Document doc = new Document();
			doc.setId(rs.getString("id"));
			doc.setTitle(rs.getString("title"));
			doc.setContent(rs.getString("content"));
			doc.setCreatetime(rs.getString("createtime"));
			doc.setEmployeeid(rs.getString("employeeid"));
			doc.setProjectid(rs.getString("projectid"));
			
			return doc;
		}
		
	}
	public void update(String id, String title, String content) {
		String sql = "update t_document set title = ? , content = ? where id = ?";
		 db.executeSQL(sql, title,content,id);
	}
	public void del(String id) {
		String sql = "delete from t_document where id = ?";
		
		db.executeSQL(sql, id);
	}
	public Document findAllById(String id) {
		String sql = "select t_document.id,`title`,content,createtime,employeeid,projectid,username from t_document,t_employee where t_document.employeeid=t_employee.id and t_document.id = ? ";
		return db.executeQueryToObject(sql, new RowMapper<Document>(){
			public Document mapRow(ResultSet rs) throws SQLException {
				Document d = new Document();
				d.setId(rs.getString("id"));
				d.setTitle(rs.getString("title"));
				d.setContent(rs.getString("content"));
				d.setCreatetime(rs.getString("createtime"));
				d.setEmployeeid(rs.getString("employeeid"));
				d.setProjectid(rs.getString("projectid"));
				
				Employee  e = new Employee();
				e.setId(rs.getString("employeeid"));
				e.setUsername(rs.getString("username"));
				
				d.setEmployee(e);
				
				return d;
			}
		},id);
	
	}
}
